#Region " Imports "

Imports BlazeApps.Library.Form
Imports System.Web.UI
Imports System.Data

#End Region

Partial Class Usercontrols_Form_FormList
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "

    Private DataPageLocal As New PagedDataSource

    Public Property sortCriteria() As String
        Get
            Return CStr(ViewState("sortCriteria"))
        End Get

        Set(ByVal Value As String)
            ViewState("sortCriteria") = Value
        End Set
    End Property

    Public Property sortDir() As String
        Get
            Return CStr(ViewState("sortDir"))
        End Get

        Set(ByVal Value As String)
            ViewState("sortDir") = Value
        End Set
    End Property

#End Region

#Region " Page Events "

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        SetPageTitle("Form List")

        If IsUserAuthenticated And (EnableUserBlogs OrElse IsEditor()) Then

            If Me.Page.IsPostBack = False Then
                LoadPage()
            End If

            ConfigBaseNavCtrls()

            If Me.Page.IsPostBack = False Then

                'Set Default Sort Order and Direction
                Me.sortCriteria = "formname"
                Me.sortDir = "asc"

                BindDataItems()
            End If

        Else
            Me.uxFormListPanel.Visible = False
            Me.uxMessagesLabel.Text = ShowControls(SecurityMask.Editor, True)
        End If

    End Sub

#End Region

#Region " Save Data "



#End Region

#Region " Load Data "

    Private Sub LoadPage()
        LoadPageSize(Me.uxPageSizeDropDownList)
    End Sub

    Public Overloads Overrides Sub BindDataItems()

        Dim List As New FormList
        Dim DT As New DataTable
        Dim DV As New DataView

        DT = List.GetForms()

        Me.RecordCount = DT.Rows.Count

        DV.Table = DT

        DV.Sort = Me.sortCriteria & " " & Me.sortDir

        Me.DataPage.DataSource = DV
        Me.DataPage.AllowPaging = True
        Me.DataPage.PageSize = CType(Me.uxPageSizeDropDownList.SelectedValue.ToString, Integer)
        Me.DataPage.CurrentPageIndex = Me.PageNumber

        CreatePagingLinks()

        With Me.uxFormGridView
            .DataSource = Me.DataPage
            .DataBind()
        End With

    End Sub

#End Region

#Region " Form Controls Events "

    Protected Sub uxAddFormButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxAddFormButton.Click
        Response.Redirect(String.Format("{0}.aspx?pid=form_formmanager", AppPage))
    End Sub

    Protected Sub DeleteForm(ByVal sender As Object, ByVal e As CommandEventArgs)
        FormDetail.DeleteFormDetail(ConvertToGuid(e.CommandArgument))
        BindDataItems()
	End Sub

	Protected Sub uxFormGridView_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles uxFormGridView.Sorting
		'Get the Sort by Value from the Sortexpression field on the column
		If Me.sortCriteria = e.SortExpression Then
			If Me.sortDir = "desc" Then
				Me.sortDir = "asc"
			Else
				Me.sortDir = "desc"
			End If
		End If

		' Assign the column clicked to the sortCriteria property
		Me.sortCriteria = e.SortExpression

		'Calls the function to rebuild the datagrid view, with the new sort order
		BindDataItems()
	End Sub

	Protected Sub uxFormGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles uxFormGridView.RowDataBound

		If e.Row.RowType = DataControlRowType.DataRow Then
			Dim lb As LinkButton = e.Row.FindControl("uxFormLinkButton")
			Dim hpl As HyperLink = e.Row.FindControl("uxFormHyperLink")

			lb.Attributes.Add("onclick", String.Format("return CopyToClipBoard('{0}')", String.Format("http://{0}{1}", Request.ServerVariables("HTTP_HOST"), hpl.NavigateUrl)))

		End If

	End Sub

#End Region

#Region " Helpers and Methods "

    Private Sub ConfigBaseNavCtrls()

        Me.PagingTopPanel = Me.uxPagingTopPanel
        Me.PagingBottomPanel = Me.uxPagingBottomPanel

        Me.RepeaterTop = Me.uxPagesTopRepeater
        Me.RepeaterBottom = Me.uxPagesBottomRepeater

        Me.NextTopLinkButton = Me.uxTopNextLinkButton
        Me.PreviousTopLinkButton = Me.uxTopPrevLinkButton
        Me.NextBottomLinkButton = Me.uxBottomNextLinkButton
        Me.PreviousBottomLinkButton = Me.uxBottomPrevLinkButton

        Me.DataPage = DataPageLocal
        Me.PageSize = CType(Me.uxPageSizeDropDownList.SelectedValue.ToString, Integer)
        Me.PagesToDisplay = 10

        Me.ItemsPerPageDropDown = Me.uxPageSizeDropDownList
        Me.ItemsPerPageLabel = Me.uxItemsPerPageLabel
        Me.PagerInfoLabel = Me.uxPagerInfoLabel

        AddHandler Me.RepeaterTop.ItemCommand, AddressOf PagingRepeater_ItemCommand
        AddHandler Me.RepeaterBottom.ItemCommand, AddressOf PagingRepeater_ItemCommand

    End Sub

#End Region

End Class
